<template>
  <div class="backStack-issue-wrap account-infos-wrap">
    <TitleCard :title="titleCard" :showMore="showMore" v-if="userType">
      <Form
        ref="formRef"
        layoutType="grid"
        rowJustify="center"
        :optionList="userInfoFromRow"
        :defaultModel="defaultModel"
        :formRules="formRules"
      ></Form>
      <div class="backStack-issue-footer-wrap">
        <div class="submit-wrap">
          <a-button class="submit-audit" @click="toAudit('formRef')"
            >保存</a-button
          >
        </div>
      </div>
    </TitleCard>
    <TitleCard :title="titleCardTwo" :showMore="showMore" v-else>
      <Form
        ref="formRef2"
        layoutType="grid"
        rowJustify="center"
        :optionList="companyFromRow"
        :defaultModel="defaultModel"
        :formRules="formRules"
      ></Form>
      <div class="backStack-issue-footer-wrap">
        <div class="submit-wrap">
          <a-button class="submit-audit" @click="toAudit('formRef2')"
            >保存</a-button
          >
        </div>
      </div>
    </TitleCard>
  </div>
</template>

<script>
import Form from '../../components/formTable/form.vue'
import mixinsBackStack from '@/mixins/backStack'
import mixinConfigSetting from '@/mixins/mixinConfigSetting'
import mixinFakeData from '@/mixins/makeFakerData'
import { userInfoFromRow, companyFromRow, formRules } from './configs/configs';
import { getAccountDetail, editUserInfo } from '@/api/accountInfos'

export default {
  mixins: [mixinsBackStack, mixinConfigSetting, mixinFakeData],
  data() {
    return {
      loading: false,
      titleCardTwo: '公司信息',
      userInfoFromRow, // 表单item配置
      companyFromRow,
      formRules, // 表单校验规则配置
      defaultModel: {},
      detailId: null,
      isRead: false,
      userType: false
    }
  },
  components: {
    Form
  },
  created() {
    this.initSetting()
  },
  methods: {
    // 初始化数据
    initSetting() {
      const { id, userType } = this.$route.query
      this.userType = userType === '01' || userType === '00' ? true : false
      this.detailId = id
      this.initDefaultModel()
    },
    // 获取默认值
    async initDefaultModel() {
      const { data, roleIds } = await getAccountDetail(this.detailId)
      this.roleIds = roleIds
      data.address = [data.province, data.city, data.area]  //Acascader格式
      data.companyAddress = [data.province, data.city, data.area, data.enpAddress]  //AcascaderInput格式
      this.handleResult(data)
    },
    handleResult(instance) {
      const { enpPic, logo, ...otherParams } = instance
      this.defaultModel = {
        ...otherParams,
        enpPic: enpPic ? [{ fileName: enpPic, filePath: enpPic, id: 1 }] : [],
        logo: logo ? [{ fileName: logo, filePath: logo, id: 1 }] : [],
      }
    },
    readChange(e) {
      const { target } = e
      this.isRead = target.checked
    },
    // 参数处理 
    beforeRequest(formRef) {
      const { detailId } = this
      let params = {}
      if (formRef === 'formRef') {
        const { userName, phonenumber, email, sex, address, intro, qq } = this.$refs[formRef].formModel
        const province = address[0]
        const city = address[1]
        const area = address[2]
        params = {
          userName, phonenumber, email, sex, province, city, area, intro, qq, id: detailId, roleIds: this.roleIds
        }
      } else {
        const { enpName, enpPhone, email, enpContact, enpType, enpNo, enpUrl, enpDate, companyAddress, enpDesc, ...formImg } = this.$refs[formRef].formModel
        const province = companyAddress[0]
        const city = companyAddress[1]
        const area = companyAddress[2]
        const enpAddress = companyAddress[3]
        const logo = this.handlerPicPathIds(formImg, 'logo').join(',')
        const enpPic = this.handlerPicPathIds(formImg, 'enpPic').join(',')
        params = {
          enpName, enpPhone, email, enpContact,
          province, city, area, enpType, enpNo,
          id: detailId, enpUrl, enpDate, enpDesc, logo, enpPic, enpAddress, roleIds: this.roleIds
        }
      }
      return params
    },
    handlerPicPathIds(instance, pic) {
      if (!instance[pic]) return []
      return instance[pic].reduce((pre, item) => {
        if (item.response) {
          const { filePath } = item.response.data[0]
          pre.push(filePath)
        }
        return pre
      }, [])
    },
    async toAudit(formRef) {
      this.loading = true
      const v = this.$refs[formRef].validatedForm()
      if (!v) return
      try {
        const params = this.beforeRequest(formRef)
        const { msg, code } = await editUserInfo(params)
        this.$apiStatusHandler(code, msg)
        if (code === 200) {
          this.$router.push({ name: 'accountInfos' })
        }
      } catch (error) {
        this.$apiStatusHandler(99, error.message)
      } finally {
        this.loading = false
      }
    },
  },
}
</script>
